package com.shxc.smarttrade.old.utils;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.shxc.smarttrade.R;

/**
 * Created by jcdev1 on 2017/6/9.
 */

public class BaseView {
    /**
     * 显示操作对话框
     *
     * @param mainView
     */
    public static PopupWindow showPopupWindow(View mainView, final Activity activity, int layout) {
        PopupWindow popupWindow = new PopupWindow(mainView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setAnimationStyle(R.style.AnimationBottonAndTop);
        popupWindow.showAtLocation(View.inflate(activity, layout, null), Gravity.BOTTOM, 0, 0);
        setBackgroundAlpha(0.7f, activity);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1f, activity);
            }
        });
        return popupWindow;
    }

    /**
     * 设置popupwindow背景色
     *
     * @param bgAlpha
     */
    public static void setBackgroundAlpha(float bgAlpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }


    public static PopupWindow showPopupWindow(View mainView, final Activity activity, int layout, int resource, int screenWidth) {
        PopupWindow popupWindow = new PopupWindow(mainView, screenWidth - 80, WindowManager.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setAnimationStyle(R.style.AnimationBottonAndTop);
        popupWindow.showAtLocation(View.inflate(activity, layout, null), resource, 0, 0);
        setBackgroundAlpha(0.6f, activity);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1f, activity);
            }
        });
        return popupWindow;
    }


    /**
     * 显示操作对话框
     *
     * @param mainView
     */
    public static PopupWindow showPopupWindow(View mainView, final Activity activity, int layout, int resource) {
        PopupWindow popupWindow = new PopupWindow(mainView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
//        popupWindow.setAnimationStyle(R.style.AnimationBottonAndTop);
        popupWindow.showAtLocation(View.inflate(activity, layout, null), resource, 0, 0);
        setBackgroundAlpha(0.7f, activity);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1f, activity);
            }
        });
        return popupWindow;
    }

    public static PopupWindow showPopupWindow(View mainView, final Activity activity, int layout, int resource, float alpha) {
        PopupWindow popupWindow = new PopupWindow(mainView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
//        popupWindow.setAnimationStyle(R.style.AnimationBottonAndTop);
        popupWindow.showAtLocation(View.inflate(activity, layout, null), resource, 0, 0);
        setBackgroundAlpha(alpha, activity);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1f, activity);
            }
        });
        return popupWindow;
    }

    /**
     * 在指定view的左下方显示
     *
     * @param mainView
     * @param activity
     * @param view2
     * @return
     */
    public static PopupWindow showPopupWindowByView(View mainView, final Activity activity, final View view2, View view3) {
        PopupWindow popupWindow = new PopupWindow(mainView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        popupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int i = popupWindow.getContentView().getMeasuredWidth();
        popupWindow.setAnimationStyle(R.style.AnimationTopAndBotton);
        popupWindow.showAsDropDown(view3, 100 - i, 10);
        setBackgroundAlpha(0.6f, activity);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1f, activity);
                ((ImageView) view2).setImageResource(R.mipmap.down_icon);
            }
        });
        return popupWindow;
    }
}
